package controllers;

import play.*;
import play.mvc.*;
import play.mvc.results.*;

import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.*;
import org.apache.mahout.cf.taste.neighborhood.*;
import org.apache.mahout.cf.taste.recommender.*;
import org.apache.mahout.cf.taste.similarity.*;
import java.io.*;
import java.util.*;


import models.*;

public class Application extends Controller {

	/**
	 * Return the value how product pass to the customer, the bigger the more like
	 * 
	 * @param customerId
	 * @param productId
	 */
	public static void itemBasedRecommendation(int customerId, int productId) {
		// get the customer profile
		// get the customers order recorders
		// get the product vector
		// calculate the value
		// using org.apache.mahout.cf.taste.similarity
		//     PreferenceInferrer->inferPreference(long userId, long itemId)
		// TODO: how to construct preference inferrer 
		// AveragingPreferenceInferrer(DataModel dataModel) 
		// AveragingPreferenceInferrer
		// GenericItemSimilarity (Pearson Correlation Score etc)
		List l = new ArrayList();
		l.add(customerId);
		l.add(productId);
		renderJSON(l);
	}
	
	public static void customerBasedRecommendation(int customerId) {
    	// get the customer id
        try {
            DataModel model =
                new FileDataModel(new File("/home/ting/Desktop/order_with_qty.csv"));
            UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
            UserNeighborhood neighborhood =
                new NearestNUserNeighborhood(100, similarity, model);
            Recommender recommender = new GenericUserBasedRecommender(
                    model, neighborhood, similarity);
            List<RecommendedItem> recommendations =
                recommender.recommend(customerId, 10);
            renderJSON(recommendations);
        } catch (Exception e) {
        	render(e);
        }
	}
	
    public static void index(int customerId) {
    }

}
